﻿namespace Common.Universal
{
    /// <summary>
    /// 返回结果的规范
    /// </summary>
    public class ApiResult
    {
        /// <summary>
        /// 错误代码
        /// </summary>
        public int Code { get; set; } = -1;

        /// <summary>
        /// 错误描述
        /// </summary>
        public string Msg { get; set; } = "发生未知错误";

        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ApiResult Error(string msg = "发生未知错误", int code = -1)
        {
            this.Code = code;
            this.Msg = msg;
            return this;
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <returns></returns>
        public ApiResult OK(string msg = "", int code = 0)
        {
            this.Code = code;
            this.Msg = msg;
            return this;
        }
    }

    /// <summary>
    /// 返回结果的规范
    /// </summary>
    /// <typeparam name="T">结果的泛型</typeparam>
    public class ApiResult<T> : ApiResult
    {
        /// <summary>
        /// 结果
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ApiResult<T> Error(string msg)
        {
            this.Code = -1;
            this.Msg = msg;
            return this;
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <returns></returns>
        public ApiResult<T> OK(T data)
        {
            this.Code = 0;
            this.Msg = "";
            this.Data = data;
            return this;
        }
    }

    /// <summary>
    /// 返回结果的规范
    /// </summary>
    /// <typeparam name="T1">结果的泛型</typeparam>
    /// <typeparam name="T2">分页信息</typeparam>
    public class ApiResult<T1, T2> : ApiResult
       where T1 : class, new()
       where T2 : class, new()
    {
        /// <summary>
        /// 结果
        /// </summary>
        public T1 Data { get; set; } = new T1();

        /// <summary>
        /// 分页信息
        /// </summary>
        public T2 Pager { get; set; } = new T2();
    }
}
